﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RTS_WebServer
{
    class RTSRequest
    {
        public Dictionary<string, string> Headers;
        public string Version;
        public string Path;
        public String[] UseableMethods =  new String[]{"POST","GET","DELETE","PUT","HEAD"};
        public String Method;
        public enum ProccesStates
        {
            METHOD,
            PATH,
            VERSION,
            HEADERS
        }
        /// <summary>
        /// State of the proccesing
        /// </summary>
        public ProccesStates State;
        /// <summary>
        /// Procceses the char.
        /// </summary>
        /// <returns></returns>
        public bool ProccesChar(char E){
            
            switch (State)
            {
                case ProccesStates.METHOD:
                    if (E == ' ') {
                        if (Method != "")
                        {
                            bool good = false;
                            foreach (string meth in UseableMethods)
                            {
                                if (meth == Method) good = true;
                            }
                            if (good)
                            {
                                State = ProccesStates.PATH;
                                return true;
                            }
                            else
                            {
                                return false;
                            }
                        }
                        else
                        {
                            return true;
                        }
                    }
                    Method += E;
                    bool Reallygood = false;
                    foreach (string meth in UseableMethods)
                    {
                        if (meth.Length >= Method.Length && meth.Substring(0, Method.Length) == Method) Reallygood = true;
                    }
                    return Reallygood;
                    break;
                case ProccesStates.PATH:
                    if(E == ' '){
                        if (Path != "")
                        {
                            State = ProccesStates.VERSION;
                        }
                        return true;
                    }
                    Path += E;
                    return true;
                    break;
                case ProccesStates.VERSION:
                    if (E == '\r' || E == '\n')
                    {
                        State = ProccesStates.HEADERS;
                        return true;
                    }
                    else
                    {
                        Version += E;
                        return true;
                    }
                case ProccesStates.HEADERS:
                    if (E == '\r' || E == '\n')
                    {
                        BusyWith = "";
                        BusyWithValue = false;
                    }
                    else if (E == ':' && !BusyWithValue)
                    {
                        BusyWithValue = true;
                        Headers.Add(BusyWith, "");
                    }
                    else
                    {
                        if (!BusyWithValue)
                        {
                            BusyWith += E;
                        }
                        else
                        {
                            Headers[BusyWith] += E;
                        }
                    }
                    break;
            }
            return true;
        }
        public string BusyWith;
        public bool BusyWithValue;
        /// <summary>
        /// Initializes a new instance of the <see cref="RTSRequest" /> class.
        /// </summary>
        public RTSRequest()
            : base()
        {
            State = ProccesStates.METHOD;
            Method = "";
            Path = "";
            Version = "";
            BusyWith = "";
            BusyWithValue = false;
            Headers = new Dictionary<string, string>();
        }

    }
}
